package com.ycode.bang.anbang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.adapter.SelectAdapter;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.model.TroubleTyoe;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 选择楼宇类别
 */
public class SelectBuildingTypeActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.listView)
    ListView mlistView;
    SelectAdapter mAdapter;
    List<TroubleTyoe> list = new ArrayList<TroubleTyoe>();
    private String villageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        ButterKnife.inject(this);
        if (this.getIntent().getExtras() != null) {
            Bundle bundle = new Bundle();
            bundle = this.getIntent().getExtras();
            villageId = bundle.getString("plotid");
        }
        initView();
    }

    public void initView() {
        getBuilding();
        title.setText("选择楼宇类别");

//        for (int i = 0; i < 8; i++) {
//            list.add(i + 1 + "幢");
//        }

        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SelectBuildingTypeActivity.this, EditAddress2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putString("plot", list.get(position).getAlias());
                bundle.putString("zhuangId", list.get(position).getValue());
                intent.putExtra("bundle", bundle);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
    }

    /**
     * 获取幢号
     */
    private void getBuilding() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
//        mAjaxParam.put("houseType", "");//可选
        mAjaxParam.put("villageId", villageId);
        FinalHttpUtil.get(RequestMapDev.GetBuildingType, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        list.clear();
                        list = gson.fromJson(data, new TypeToken<List<TroubleTyoe>>() {
                        }.getType());
                        mAdapter = new SelectAdapter(SelectBuildingTypeActivity.this, list);
                        mlistView.setAdapter(mAdapter);
                    } else {
                        ToastUtil.show(SelectBuildingTypeActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(SelectBuildingTypeActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(SelectBuildingTypeActivity.this, strMsg);
            }
        });
    }
}
